define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'district', 'districts'], function ($, undefined, Backend, Table, Form, District, Districts) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'user/user/index',
                    add_url: 'user/user/add',
                    edit_url: 'user/user/edit',
                    //del_url: 'user/user/del',
                    multi_url: 'user/user/multi',
                    table: 'user',
                }
            });

            var table = $("#table");
            table.on('post-common-search.bs.table', function (event, table) {
                var form = $("form", table.$commonsearch);
                $("input[name='store_id']", form).addClass("selectpage")
                    .data("source", "store/store/index")
                    .data("primaryKey", "id")
                    .data("field", "name")
                    .data("orderBy", "id desc");
                Form.events.cxselect(form);
                Form.events.selectpage(form);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'user.id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'), sortable: true},
                        //{field: 'username', title: __('Username'), operate: 'LIKE'},
                        {field: 'number', title: __('Number'), operate: false},
                        {field: 'nickname', title: __('Nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('Mobile'), operate: 'LIKE'},
                        {field: 'avatar', title: __('Avatar'), events: Table.api.events.image, formatter: Table.api.formatter.image, operate: false},
                        {field: 'user_type', title: __('User_type'), formatter: Table.api.formatter.flag, searchList: userTypeList},
                        {field: 'store_id', title: __('Store_id'), visible: false},
                        {field: 'store_name', title: __('Store_id'),
                            //hidden: function(value, row, index){
                            //    if(row.user_type == 1){
                            //        return true;
                            //    }
                            //    return false;
                            //},
                            operate:false
                        },
                        {field: 'address', title: __('City'),
                            hidden: function(value, row, index){
                                if(row.user_type == 1){
                                    return true;
                                }
                                return false;
                            },
                            operate: false
                        },
                        //{field: 'circle_name', title: __('所在商圈'), operate: false},
                        {field: 'audit.status', title: __('是否认证'),
                            formatter: function(value, row, index){
                                if(value == 4){
                                    return "<span style='color: red;'>已认证,已过期</span>";
                                }else if(value == 2){
                                    return "已认证,过期时间"+row.expiration_time;
                                }else{
                                    return "未认证";
                                }
                            },
                            searchList:{'1':'未认证',"2":"已认证"}
                        },
                        {field: 'industry', title: __('Industry'), operate: 'LIKE'},
                        //{field: 'gender', title: __('Gender'), visible: false, searchList: {1: __('Male'), 0: __('Female')}},
                        {field: 'status', title: __('Status'), formatter: Table.api.formatter.status, searchList: {normal: __('Normal'), hidden: __('禁用')}},
                        {field: 'createtime', title: __('Createtime'), formatter: Table.api.formatter.datetime, operate: 'RANGE'},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
                var user_type = $('select[name="row[user_type]"] option:selected').val();
                console.log(user_type);
                Controller.api.typeChange(user_type);
                $(document).on('change', 'select[name="row[user_type]"]', function(){
                    Controller.api.typeChange($(this).val());
                });

                var area_id = $("select[name='row[area_id]']").data('id');
                var circle_area_id = $("select[name='row[area_id]']").data('id');
                //console.log(area_id);
                $("input[name='row[circle_id]']").data('params', function(){
                    return {custom:{area_id:circle_area_id}};
                });
                $("input[name='row[store_id]']").data('params', function(){
                    return {custom:{district_id:area_id}};
                });
                $(document).on('change', '.district select.district', function(){
                    area_id = $(this).val();
                    $("input[name='row[store_id]']").selectPageClear();
                    //$("input[name='row[circle_id]']").selectPageClear();
                });
                $(document).on('change', '.districts select.district', function(){
                    circle_area_id = $(this).val();
                    $("input[name='row[circle_id]']").selectPageClear();
                });

                District.districtCpcdsEvent({type:1});
                Districts.districtCpcdsEvent({type:2,class_name:'districts'});
            },
            typeChange: function(user_type){
                if(user_type == 1){
                    if(!$('.user_type_vip').hasClass('hide')){
                        $('.user_type_vip').addClass('hide');
                    }
                }else{
                    if($('.user_type_vip').hasClass('hide')){
                        $('.user_type_vip').removeClass('hide')
                    }
                }
            }
        }
    };
    return Controller;
});